/* ------------------------------------------------------------------------
// usos generales
-------------------------------------------------------------------------*/ 
function addEventToObject(obj,evt,func) {
	var oldhandler = obj[evt];
	obj[evt] = (typeof obj[evt] != 'function') ? func : function(ev){oldhandler(ev);func(ev);};
}

function stopDefaultAction(ev) {
	if (!ev) ev = window.event;
	(ev.stopPropagation) ? ev.stopPropagation() : ev.cancelBubble = true;
	(ev.preventDefault) ? ev.preventDefault() : ev.returnValue = false;
	return false;
}

// mostrar|ocultar capa de selección de fecha final
function ends_date_layer(checked) {
    if($("ends_at_select")){
        if(checked == true) $("ends_at_select").show();
        else  $("ends_at_select").hide();
    }
}


// posiciona una capa absoluta con su _locator
function locate_div(layer) {
    if($(layer) && $(layer+"_locator")) {
        $(layer).show()
        var pos = Element.cumulativeOffset($(layer+"_locator"))
        $(layer+"_locator").style.height = ($(layer).offsetHeight+10)+"px"
        $(layer).style.top = pos[1]+"px"
        $(layer).style.left = pos[0]+"px"
    }
}


// mapas en creación
// pendiente de refactorizar
var map;
var center;
var marker;
var address = "";

function initialize_map(lat, long, drag) {
    if($("map")) {
        map = new google.maps.Map2(document.getElementById("map"));
        // marcamos el centro aquí por ponerlo en algún sitio
        if(lat && long) {
            center = new google.maps.LatLng(lat,long);
        } else {
            center = new google.maps.LatLng(40.453955,-3.700356);
        }
        map.addControl(new GSmallMapControl());
        map.setCenter(center, 16);
        if(lat && long) {
            if(drag) {
                marker = new GMarker(center, {draggable: true});
                GEvent.addListener(marker, "dragstart", function() {
                  });
                GEvent.addListener(marker, "dragend", function() {
                    set_map();
                  });
                map.addOverlay(marker);
                
            } else {
                marker = new GMarker(center);
                map.addOverlay(marker);
            }
        } else {
            marker = new GMarker(center, {draggable: true});
            GEvent.addListener(marker, "dragstart", function() {
              });
            GEvent.addListener(marker, "dragend", function() {
                set_map();
              });
            map.addOverlay(marker);
        
            if(address != "") {
                showAddress(address);
                address="";
            }
        }
        
    }
}

function get_address() {
  if ($('place_address').value == "") {
       address = "";
  } else { 
    address = $('place_address').value;
    address += ", " + $('place_city').value;
    address += ", " + $('place_state').value;
    address += ", " + $('place_country_id').options[$('place_country_id').selectedIndex].text;
  }
}

function set_map() {
    $("place_latitude").value = marker.getLatLng().lat();
    $("place_longitude").value = marker.getLatLng().lng();
}

function nullify_map() {
    $("place_latitude").value = "";
    $("place_longitude").value = "";
}


function showAddress(address) {
  var geocoder = new GClientGeocoder();
  geocoder.getLatLng(
    address,
    function(point) {
      if (!point) {
        // alert(address + " no encontrada");
      } else {
        map.setCenter(point, 16);
        marker.setLatLng(point);
        
        set_map();
      }
    }
  );
}


/* ------------------------------------------------------------------------
// Burbuja 
-------------------------------------------------------------------------*/

var bubbleAvatar = {
    visibilidad: 0,

    show : function(login, nombre)
    {
        var cmb = bubbleAvatar;

        if ($('bubble')) {
            var combo = $('bubble');
            var origavatar = $('avatar_'+login);
            var contenedor = $('bubble_cont');

            var contenido = '<a href="/user/'+ login +'">'+ nombre + '</a>';

            contenedor.innerHTML = contenido;

            // posicion del avatar
            var posavatar = Position.cumulativeOffset(origavatar);
            combo.style.visibility = 'visible';
            combo.style.left = (posavatar[0] - 12) + 'px';
            combo.style.top = (posavatar[1] - origavatar.offsetHeight - combo.offsetHeight - 5) + 'px';

            cmb.visibilidad = 1;
        }
    },
    hide : function()
    {
        var cmb = bubbleAvatar;
        if ($('bubble')) $('bubble').style.visibility = 'hidden';
        cmb.visibilidad = 0;
    }

}

// cancelamos la burbuja
Event.observe(document, 'click', function(e){ var elem = (e.target) ? e.target : e.srcElement; if(elem.parentNode.className != 'bubble') {bubbleAvatar.hide();}}, false);

// definimos eventos
addEventToObject(window,'onresize',bubbleAvatar.hide);
